package net.sky.oa.dao.ioffice.impl;

import java.util.List;
import java.util.Date;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.htsoft.core.web.paging.PagingBean;

import net.sky.oa.model.OffTask;
import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.ioffice.OffTaskDao;


@SuppressWarnings("unchecked")
@Repository
public class OffTaskDaoImpl extends SkyBaseDaoImpl<OffTask> implements OffTaskDao {

	public OffTaskDaoImpl() {
		super.setPersistType(OffTask.class);
	}
	
	public List<OffTask> findByRusr(Long rusrId,int rank,String title,Date beginTime,Date endTime,PagingBean pb){
		StringBuilder sb=new StringBuilder("select ot from OffTask ot,OffTaskUser tu where ot.id=tu.task.id and ot.status=1 and tu.rusr.id=? ");
		List list=new ArrayList();
		list.add(rusrId);
		try{
			if(rank!=-1)
			{
				sb.append(" and ot.rank=? ");
				list.add(rank);
			}
			if(StringUtils.isNotEmpty(title))
			{
				sb.append(" and ot.title like ?");
				list.add("%"+title+"%");
			}
			if(null!=beginTime)
			{
				sb.append(" and ot.begintime > ?");
				list.add(beginTime);
			}
			if(null!=endTime)
			{
				sb.append(" and ot.endtime < ?");
				list.add(endTime);
			}
			sb.append(" order by ot.id desc");
			return findByHql(sb.toString(),list.toArray(),pb);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public OffTask findById(Long id,Long rusrId,String sort,PagingBean pb){
		StringBuilder sb=new StringBuilder("select ot from OffTask ot,OffTaskUser tu where ot.id=tu.task.id and ot.status=1 and tu.rusr.id=? ");
		List list=new ArrayList();
		list.add(rusrId);
		try{
			if(StringUtils.isEmpty(sort))
			{
				sb.append(" and ot.id = ? ");
			}
			if("asc".equals(sort))
			{
				sb.append(" and ot.id > ?  order by ot.id");
			}
			if("desc".equals(sort))
			{
				sb.append(" and ot.id < ?  order by ot.id desc");
			}			
			list.add(id);

			List<OffTask> l=findByHql(sb.toString(),list.toArray(), pb);
			if(l.size()>0)
				return l.get(0);
			else
				return null;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
